<?php

namespace App\Controller\Api\User;

use App\Constants\ErrorCode;
use App\Controller\Api\BaseController;
use App\Model\ChatgptMessage;
use App\Model\ConsultantDatum;
use App\Model\SysSetting;
use App\Service\ChatGptService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;

/**
 *chatGPT控制器类
 * @Middleware(JWTAuthMiddleware::class)
 * @Controller(prefix="/user/v1/chatai")
 */
class ChatAiController extends BaseController
{

    /**
     * @Inject()
     * @var ChatGptService
     */
    public $chatGptService;

    /**
     * @RequestMapping(path="askQuestion")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function askQuestion()
    {

        $uid = $this->uid();

        $params = $this->request->post();

        $this->validate($params, [
            'message' => 'required'
        ]);


        $userInfo = userInfo($uid);

        if ($userInfo['role'] != 1) {//不是会员 去判断免费次数
            $sysInfo = SysSetting::sysInfo();

            $chatNum = ChatgptMessage::where(['uid' => $uid])->count();

            if ($chatNum > $sysInfo['chatGptFreeNum']) {
                return $this->response->returnFail(ErrorCode::NOT_SUBSCRIBE);
            }

        }

        $result = $this->chatGptService->sendMessage($userInfo['num'], $params['message'], $uid);


        if ($result['code'] == 200) {

            $help = 0;
            if ($this->helpStatus($this->uid())){
                $help = 1;
            }

            return $this->response->returnSuccess(['reply' => $result['message'], 'help' => $help]);
        } else {
            return $this->response->returnFail();
        }


    }



    public function helpStatus($uid)
    {

        $key = 'chatGptNum:' . $uid;
        $key1 = 'chatGptPrompt:' . $uid;

        $prompt = redis()->get($key1);

        if (!$prompt){
            $prompt =3;
        }

        $num = redis()->get($key);

        if (!$num) {
            redis()->set($key, 1, 1800);
            redis()->set($key1, 3);
        } else {

            if ($num >= $prompt) {
                redis()->del($key);

                $prompt = rand(3, 8);
                redis()->set($key1, $prompt);

                return true;
            } else {
                redis()->incr($key);
            }
        }
        return false;


    }


    /**
     * @RequestMapping(path="aiRecommend")
     * @return ResponseInterface
     */
    public function aiRecommend()
    {

        //todo 后期根据发送的内容和选择的类型 去推荐咨询师

        $list = ConsultantDatum::getRecommendConsultantList();

        return $this->response->returnSuccess(['list' => $list]);


    }


    /**
     * @RequestMapping(path="init")
     * @return ResponseInterface
     */
    public function init()
    {

        $info = ChatgptMessage::where(['uid' => $this->uid(), 'type' => 2])->orderByDesc('id')->first();

        if ($info) {
            return $this->response->returnSuccess(['reply' => $info['message'], 'help' => 1]);
        } else {
            return $this->response->returnSuccess(['reply' => '', 'help' => 1]);
        }
    }


    /**
     * @RequestMapping(path="messageList")
     * @return ResponseInterface
     */
    public function messageList()
    {


        $model = ChatgptMessage::where(['uid' => $this->uid()])->orderByDesc('id');

        $list = page($model);

        return $this->response->returnSuccess(['list' => $list]);


    }


}
